<?php
/**
 *
 * This file contains {@link Domain51_Service_Delicious_Posts}
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */


/**
 *
 * Required interface for connection
 *
 */
Solar::loadClass('Domain51_Util_Service_Connection');


/**
 *
 * The object that handles formatting requests for tags API calls to 
 * del.icio.us
 *
 * This class should not be instantiated directly when used during normal use,
 * instead it should be referenced via {@link Domain51_Service_Delicious}.
 *
 *
 * @package Domain51
 *
 * @subpackage Domain51_Service
 *
 * @author Travis Swicegood <development [at] domain51 [dot] com>
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 * @see Domain51_Service_Delicious
 *
 */ 
class Domain51_Service_Delicious_Tags  
{
    /**
     *
     * The {@link Domain51_Util_Service_Connection} object passed in at 
     * instantiation.
     *
     * @var Domain51_Util_Servce_Connection
     *
     * @access private
     *
     * @see __construct(), Domain51_Util_Servce_Connection
     *
     */
    private $_connection = null;
    
    
    /**
     *
     * Handle instantiation
     *
     *
     * @param Domain51_Util_Service_Connection $connection
     *
     * @see Domain51_Service_Delicious
     *
     */
    public function __construct(Domain51_Util_Service_Connection $connection) {
        $this->_connection = $connection;
    }
    
    
    /**
     *
     * Serves as the Stragetgy object for determining what type of call this is
     * and returning the proper class.
     *
     * This will return one of the objects from the 
     * Domain51_Service_Delicious_Tags_* objects depending on the method.
     *
     * @param string $method
     *
     * @param array $arguments
     *
     * @return object
     *
     */
    public function __call($method, $arguments) {
        switch ($method) {
            case 'bundles' :
                $adapter = ucwords($method);
                break;
            
            default:
                throw $this->_exception('Unknown method request');
        }
        
        $adapterClass = 'Domain51_Service_Delicious_Tags_' . $adapter;
        Solar::loadClass($adapterClass);
        return new $adapterClass($this->_connection); 
    }
    
    
    /**
     *
     * Returns an array of tags and number of times used by the user by 
     * performing a <code>tags/get</code> API call.
     *
     *
     * @return array
     *
     */
    public function get() {
        $uri = 'tags/get';
        $responseXml = $this->_connection->doRequest($uri);
        
        $array = array();
        foreach ($responseXml->tag as $tag) {
            $tagName = (string)$tag['tag'];
            $tagCount = (int)$tag['count'];
            $array[$tagName] = array(
                'tag' => $tagName,
                'count' => $tagCount
            );
        }
        
        return $array;
    }
    
    
    /**
     *
     * Rename an existing tag with a new tag name by performing a 
     * <code>tags/rename</code> API call.
     *
     * The <code>tags/rename</code> call always returns "done", so this method
     * will always return <code>true</code> until that API return is changed.
     * 
     * @param string $old The tag name to change
     *
     * @param string $new The new tag name
     *
     * @return bool
     *
     */
    public function rename($old, $new) {
        $encodedOld = urlencode($old);
        $encodedNew = urlencode($new);
        $uri = "tags/rename?old={$encodedOld}&new={$encodedNew}";
        
        $responseXml = $this->_connection->doRequest($uri);
        return ((string)$responseXml == 'done');
    }
}

?>